﻿@inherits AdminCompontentBase
@page "/alarmHistory"
@page "/alarmHistory/{AlarmRuleId}"
<PageTitle>@T("AlarmHistory")</PageTitle>
<AutoHeight AutoClass="mt-6">
    <HeaderContent>
        <SSearchPanel @bind-Value="@_queryParam.Filter" Advanced OnEnter="RefreshAsync" OnClearClick="HandleClearAsync">
            <ChildContent>
                <SLabeledRadioGroup @bind-Value="@_queryParam.SearchType" ActiveClass="fill-background" Class="white" Dense OnChange="HandleSearchTypeChange">
                    @foreach (var item in Enum.GetValues<AlarmHistorySearchTypes>())
                    {
                        <SLabeledRadio Value="item" Class="px-6">
                            <span class="@(_queryParam.SearchType == item ? "subtitle2 emphasis2--text" : "btn regular2--text")">@T(item.ToString())</span>
                        </SLabeledRadio>
                    }
                </SLabeledRadioGroup>
            </ChildContent>
            <AdvancedContent>
                <MCol Md="2">
                    <SSelect Small @bind-Value="@_queryParam.TimeType"
                             Items="_timeTypeItems"
                             Label="@T(nameof(_queryParam.TimeType))"
                             ItemText="e => T(e==default?string.Empty:e.ToString())"
                             ItemValue="e=> e"
                             BackgroundColor="white"
                             Clearable
                             TItem="AlarmHistorySearchTimeTypes"
                             TItemValue="AlarmHistorySearchTimeTypes"
                             TValue="AlarmHistorySearchTimeTypes"
                             Class="rounded-lg body2"
                             OnSelectedItemUpdate="RefreshAsync"
                             OnClearClick="RefreshAsync">
                    </SSelect>
                </MCol>
                    <SDateTimeRangeToolbar Class="ml-6" StartDateTime="_queryParam.StartTime" EndDateTime="_queryParam.EndTime" OnUpdate="DateRangChangedAsync" ShowQuickChange="false" />
                <MCol Md="2" Class="ml-6">
                    <SSelect Small @bind-Value="@_queryParam.AlertSeverity"
                             Items="Enum.GetValues<AlertSeverity>().ToList()"
                             Label="@T(nameof(_queryParam.AlertSeverity))"
                             ItemText="e => T(e==default?string.Empty:e.ToString())"
                             ItemValue="e=> e"
                             BackgroundColor="white"
                             Clearable
                             TItem="AlertSeverity"
                             TItemValue="AlertSeverity"
                             TValue="AlertSeverity"
                             Class="rounded-lg body2"
                             OnSelectedItemUpdate="RefreshAsync"
                             OnClearClick="RefreshAsync">
                    </SSelect>
                </MCol>
                @if (_queryParam.SearchType == AlarmHistorySearchTypes.Alarming)
                {
                    <MCol Md="2" Class="ml-6">
                        <SSelect Small @bind-Value="@_queryParam.HandleStatus"
                             Items="_handleStatusItems"
                             Label="@T(nameof(_queryParam.HandleStatus))"
                             ItemText="e => T(e==default?string.Empty:e.ToString())"
                             ItemValue="e=> e"
                             BackgroundColor="white"
                                      Clearable
                             TItem="AlarmHistoryHandleStatuses"
                             TItemValue="AlarmHistoryHandleStatuses"
                             TValue="AlarmHistoryHandleStatuses"
                             Class="rounded-lg body2"
                             OnSelectedItemUpdate="RefreshAsync"
                             OnClearClick="RefreshAsync">
                        </SSelect>
                    </MCol>
                }
                @if (_queryParam.SearchType == AlarmHistorySearchTypes.Processed)
                {
                    <MCol Md="2" Class="ml-6">
                        <SUserAutoComplete Small @bind-Value="_queryParam.Handler" Label="@(T("Handler"))" FillBackground="false" OnSelectedItemUpdate="RefreshAsync" Clearable />
                    </MCol>
                }
            </AdvancedContent>
        </SSearchPanel>
    </HeaderContent>
    <AutoHeightContent>
        <MCard Class="full-height">
            <SDataTable Headers="GetHeaders()" Items="_entities.Result" TItem="AlarmHistoryListViewModel" ItemsPerPage="_queryParam.PageSize" OnOptionsUpdate="HandleOnOptionsUpdate" HideDefaultFooter FixedRight @ref="_dataTableRef" Class="d-flex full-height flex-column">
                <HeaderColContent Context="header">
                    <span class="text-btn">@header.Text</span>
                </HeaderColContent>
                <ItemColContent>
                    @switch (context.Header.Value)
                    {
                        case nameof(AlarmHistoryListViewModel.AlertSeverity):
                            <AlertSeverityTag Value="@context.Item.AlertSeverity" />
                            break;
                        case nameof(AlarmHistoryListViewModel.HandleStatus):
                            <AlarmHistoryStatusTag Value="@context.Item.HandleStatus" />
                            break;
                        case nameof(AlarmHistoryQueryModel.RecoveryTime):
                            <MListItem Class="pa-0">
                                <MListItemContent>
                                    <MListItemTitle Class="body regular--text">@context.Item.Handle.HandlerName @context.Item.RecoveryTime?.ToOffset(JsInitVariables.TimezoneOffset).ToString(T("$TimeFormat"))</MListItemTitle>
                                    <MListItemSubtitle Class="body regular--text">@context.Item.RecoveryTime?.ToOffset(JsInitVariables.TimezoneOffset).ToString(T("$DateFormat"))</MListItemSubtitle>
                                </MListItemContent>
                            </MListItem>
                            break;
                        case "Action":
                            @if (context.Item.HandleStatus == AlarmHistoryHandleStatuses.Pending && _queryParam.SearchType!= AlarmHistorySearchTypes.NoNotice)
                            {
                                <MButton Class="mr-4" Icon OnClick="()=>_handleAlarmModal?.OpenModalAsync(context.Item)!">
                                    <SIcon Size="20" Small Color="emphasis2" Tooltip="@(T("Handle"))">mdi-account-check</SIcon>
                                </MButton>
                            }
                            else
                            {
                                <MButton Class="mr-4" Icon OnClick="()=>_detailModal?.OpenModalAsync(context.Item)!">
                                    <SIcon Size="20" Small Color="emphasis2" Tooltip="@(T("View"))">mdi-eye</SIcon>
                                </MButton>
                            }
                            break;
                        default:
                            <DefaultGenericColumnRender Value="@context.Value" Width="@context.Header.Width"></DefaultGenericColumnRender>
                            break;
                    }
                </ItemColContent>
                <FooterContent>
                    <SPagination Class="mt-3 mx-6 pb-6" Page="@_queryParam.Page" PageSize="@_queryParam.PageSize" Total="_entities.Total" PageChanged="HandlePageChanged" PageSizeChanged="HandlePageSizeChanged" />
                </FooterContent>
            </SDataTable>
        </MCard>
    </AutoHeightContent>
</AutoHeight>

<AlarmHistoryDetailModal @ref="_detailModal" OnOk="HandleOk"/>
<HandleAlarmModal @ref="_handleAlarmModal" OnOk="HandleOk"/>